﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using TwitterMatic.Shared.Domain;

namespace TwitterMatic_WebRole.Code
{
    public static class JsonTimeline
    {
        public static string Create(IEnumerable<TimedTweet> tweets)
        {
            // Create a new list of JsonTimelineEvent
            List<JsonTimelineEvent> events = new List<JsonTimelineEvent>();
            foreach (var tweet in tweets)
            {
                string slug = tweet.Status;
                if (slug.Length >= 10)
                {
                    slug = slug.Substring(0, 10) + "...";
                }

                events.Add(new JsonTimelineEvent {
                    start = tweet.SendOn.ToString(),
                    title = slug,
                    description = tweet.Status
                });
            }

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return "{'events' : " + serializer.Serialize(events) + "}";
        }
    }

    public class JsonTimelineEvent
    {
        public string start { get; set; }
        public string title { get; set; }
        public string description { get; set; }
    }
}
